var getTest = toContextPath('/student/test_getById.action');
var findStuInfo = toContextPath('/student/student_getInfo.action');
var findExamInfo = toContextPath('/student/exam_getById.action');
var loginHtml = toContextPath('/StuPages/login.html');

$(function() {
	getExam();
	countDown();
});
function countDown(hours, minutes, seconds) {
	$('#timer').text("Time Left:" + hours+':'+minutes+':'+seconds);
	setTimeout(function () {
        countDown(hours, minutes, seconds);
    }, 1000);
}

function getExam() {
	var array = location.href.split("?");
	var paras = array[1].split("&");
	var testId = window.parseInt(paras[0].substring(7, paras[0].length));
	var examId = window.parseInt(paras[1].substring(7, paras[1].length));
	
	$.getJSON(findStuInfo, function(data) {
		if (data.success) {
			$('#stuName').text(data.result.stuName);
		} else {
			window.location.href = loginHtml;
			alert("You need to login first!");
		}
	});
	
	
	$.getJSON(findExamInfo, {examId:examId}, function(data) {
		if (data.success) {
			$('#examName').text(data.result.examName);
			$('#examDate').text(data.result.examBeginDate);
			if (data.result.state == 2) {
				$('#timer').hide();
				$("input").prop('disabled', true);
			} else if (data.result.state == 1) {
				countDown(data.result.examHour, 0, 0);
			}	
			
		} else 
			alert("You need to login first!");
	});
	$.getJSON(getTest, {testId:testId}, function(data) {
		if (data.success) {
			$('#listenDirections').text(data.result.listen.listenDirection);
			$('#listenPassage').text(data.result.listen.listenPassage);
			$('#selectionDirections').text(data.result.selection.selectionDirections);
			$('#selectionPassage').text(data.result.selection.selectionPassge);
			$('#selections').text(data.result.selection.selections);
			$('#translateDirections').text(data.result.translate.translateDirection);
			$('#translatePassage').text(data.result.translate.translateTitle);
			$('#rewriteDirections').text(data.result.rewrite.rewriteDirection);
			$('#rewriteSentences').text(data.result.rewrite.rewriteTitle);
			$('#readDirections').text(data.result.read.readDirection);
			$('#readPassage').text(data.result.read.readI);
			$('#writeContent').text(data.result.write.writeContent);
		} else 
			alert("You need to login first!");
	});
}